﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace slimPickins.Tools.PropParser
{
    public class BuildPropParser
    {
        string pathToFile;

        public struct propValuesStruct
        {
            internal string Name { get; set; }
            internal string Value { get; set; }
            internal bool isComment { get; set; }
        }

        public List<propValuesStruct> propValues = new List<propValuesStruct>();

        public BuildPropParser(string path)
        {
            pathToFile = path;

            Read();
        }

        void Read()
        {
            
            if (File.Exists(pathToFile))
            {
                StreamReader PropFileReader = File.OpenText(pathToFile);

                while (PropFileReader.Peek() != -1)
                {
                    if (PropFileReader.Peek() != '#')
                    {
                        //string[] section;
                        bool comment = false;
                        string line;
                        //char delimiter = '=';

                        line = PropFileReader.ReadLine();


                        if (line.StartsWith("#"))
                        {
                            comment = true;
                            //addToList(comment, line);
                        }
                        //else
                            
                        if(line != "")
                            addToList(comment, line);


                            
                        
                    }
                    else
                        PropFileReader.ReadLine();
                }
                

            }
        }

        void addToList(bool isComment, string values)
        {
            if (values != null || values != "")
            {
                propValuesStruct tmpStruct = new propValuesStruct();
                if (!isComment)
                {
                    string[] splitValues = values.Split('=');
                    tmpStruct.Name = splitValues[0];
                    tmpStruct.Value = splitValues[1];
                }
                else
                {
                    tmpStruct.Value = values;
                }

                if (tmpStruct.Value.StartsWith(" "))
                    tmpStruct.Value = tmpStruct.Value.Remove(0, 1);

                tmpStruct.isComment = isComment;

                propValues.Add(tmpStruct);
            }
        }

        internal string getRomVersion()
        {
            propValuesStruct result = propValues.Find(delegate(propValuesStruct value)
            {
                return value.Name == "ro.build.display.id";
            }
            );

            return result.Value;
        }

        internal string getRomName()
        {
            propValuesStruct result = propValues.Find(delegate(propValuesStruct value)
            {
                return value.Name == "ro.build.description";
            }
            );

            return result.Value;
        }
    }
}
